package com.smsVerification.utils;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;

import java.text.DecimalFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;


abstract public class DateTimeValueHandler {

    static public String getTimeDescription(Date time) {
        // 拿到当前时间戳和发布时的时间戳，然后得出时间戳差
        Date curTime = new Date();
        long timeDiff = curTime.getTime() - time.getTime();

        // 单位换算
        double min = 60 * 1000;
        double hour = min * 60;
        double day = hour * 24;
        double week = day * 7;
        double month = week * 4;
        double year = month * 12;
        DecimalFormat df = new DecimalFormat("#");
        // 计算发布时间距离当前时间的周、天、时、分
        double exceedYear = Math.floor(timeDiff / year);
        double exceedMonth = Math.floor(timeDiff / month);
        double exceedWeek = Math.floor(timeDiff / week);
        double exceedDay = Math.floor(timeDiff / day);
        double exceedHour = Math.floor(timeDiff / hour);
        double exceedMin = Math.floor(timeDiff / min);


        // 最后判断时间差到底是属于哪个区间，然后return

        if (exceedYear < 100 && exceedYear > 0) {
            return df.format(exceedYear) + "年前";
        } else {
            if (exceedMonth < 12 && exceedMonth > 0) {
                return df.format(exceedMonth) + "月前";
            } else {
                if (exceedWeek < 4 && exceedWeek > 0) {
                    return df.format(exceedWeek) + "星期前";
                } else {
                    if (exceedDay < 7 && exceedDay > 0) {
                        return df.format(exceedDay) + "天前";
                    } else {
                        if (exceedHour < 24 && exceedHour > 0) {
                            return df.format(exceedHour) + "小时前";
                        } else {
                            if (exceedMin <= 0) {
                                return "刚刚";
                            } else {
                                return df.format(exceedMin) + "分钟前";
                            }
                        }
                    }
                }
            }
        }
    }

    static public String getTimeDescription(String timeString) throws ParseException {

        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date time = simpleDateFormat.parse(timeString);

        return getTimeDescription(time);
    }

    /**
     * @return 明天的 00:00:01 时
     */
    static public Date getRedisMidnightExpiredTime() {
        //明天
        DateTime dateTime = DateUtil.tomorrow();
        Calendar calendar = dateTime.toCalendar();

        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 1);
        calendar.set(Calendar.MILLISECOND, 0);

        return calendar.getTime();
    }
}
